<?php
class expand_model_fieldModel extends baseModel{
    protected $table = 'expand_model_field';

    //字段列表
    public function field_list_data($mid)
    {
        return $this->select('mid='.$mid,'','sequence asc,fid asc');
    }


    //字段列表
    public function field_list($mid)
    {
        $list=$this->field_list_data($mid);
        if(empty($list)){
            return;
        }
        foreach ($list as $key => $vo) {
            $data[$key]=$vo;
            $data[$key]['type_name']=$this->field_type($vo['type'],true);
        }
        return $data;
    }

    //获取字段类型名称
    public function field_type($id=null,$name=false)
    {
        $list=array(
            1=> array(
                'name'=>'文本框',
                'property'=>1,
                ),
            2=> array(
                'name'=>'多行文本',
                'property'=>3,
                ),
            3=> array(
                'name'=>'编辑器',
                'property'=>3,
                ),
            4=> array(
                'name'=>'文件上传',
                'property'=>1,
                ),
            10=> array(
                'name'=>'单图片上传',
                'property'=>1,
                ),
            5=> array(
                'name'=>'组图上传',
                'property'=>3,
                ),
            6=> array(
                'name'=>'下拉菜单',
                'property'=>3,
                ),
            7=> array(
                'name'=>'日期和时间',
                'property'=>2,
                ),
            11=> array(
                'name'=>'货币',
                'property'=>4,
                ),
            8=> array(
                'name'=>'单选',
                'property'=>3,
                ),
            9=> array(
                'name'=>'多选',
                'property'=>3,
                ),
        );
        if(!empty($id)){
            if($name){
                return $list[$id]['name'];
            }else{
                return $list[$id];
            }
        }else{
            return $list;
        }
    }

    //获取字段属性
    public function field_property($id=null,$name=false)
    {
        $list=array(
            1=> array(
                'name'=>'varchar',
                'maxlen'=>250,
                'decimal'=>0,
                ),
            2=> array(
                'name'=>'int',
                'maxlen'=>10,
                'decimal'=>0,
                ),
            3=> array(
                'name'=>'text',
                'maxlen'=>0,
                'decimal'=>0,
                ),
            4=> array(
                'name'=>'decimal',
                'maxlen'=>10,
                'decimal'=>0,
                ),
        );
        if($name){
            return $list[$id]['name'];
        }else{
            return $list[$id];
        }
    }

    //获取字段信息
    public function field_info($field,$mid,$fid=null) {
        $where='field="'.$field.'"';
        $where.=' AND mid='.$mid;
        if(!empty($fid)){
        $where.=' AND fid<>'.$fid;
        }
        return $this->find($where);
    }

    //获取字段信息通过ID
    public function field_info_id($fid) {
        $where['fid']=$fid;
        return $this->find($where);
    }

    //格式化字段
    public function field_data($data) {
        $type=$data['type'];
        $type_info=$this->field_type($type);
        $property_info=$this->field_property($type_info['property']);

        $data['fild_name']=$property_info['name'];
        $data['len']=$property_info['maxlen'];
        if($property_info['decimal']){
            $data['decimal']=','.$property_info['decimal'];
        }
        $data['verification']=serialize($data['verification']);
        $data['default']=html_in($data['default']);
        $data['config']=html_in($data['config']);
        return $data;
    }

    public function field_add($data) {
        $model=model('expand_model')->info($data['mid']);
        $data=$this->field_data($data);
        //添加真实字段
        $sql="
        ALTER TABLE {$this->model->pre}expand_content_{$model['table']} ADD {$data['field']} {$data['fild_name']}({$data['len']}{$data['decimal']}) DEFAULT NULL
        ";
        $this->model->query($sql);
        return $this->insert($data);
        
    }

    public function field_edit($data) {
        $info=$this->field_info_id($data['fid']);
        $model=model('expand_model')->info($data['mid']);
        $data=$this->field_data($data);
        //添加真实字段
        $sql="
        ALTER TABLE {$this->model->pre}expand_content_{$model['table']} CHANGE {$info['field']} {$data['field']} {$data['fild_name']}({$data['len']}{$data['decimal']})
        ";
        $this->model->query($sql);
        $condition['fid']=intval($data['fid']);
        return $this->update($condition,$data); 
    }

    //字段删除
    public function field_del($data)
    {
        $model=model('expand_model')->info($data['mid']);
        $info=$this->field_info_id($data['fid']);
        $sql="
             ALTER TABLE {$this->model->pre}expand_content_{$model['table']} DROP {$info['field']}
            ";
        $this->model->query($sql);
        $condition['fid']=intval($info['fid']);
        return $this->delete($condition);
    }

    //通过模型删除
    public function model_del($mid)
    {
        $condition['mid']=intval($info['mid']);
        return $this->delete($condition);
    }

    //获取扩展内容字段数据
    public function get_file_content($aid,$mid)
    {
        if(empty($aid)||empty($mid)){
            return;
        }
        //获取字段信息
        $model=model('expand_model')->info($mid);
        //获取扩展表内容
        $condition['aid']=$aid;
        return $this->model->table('expand_content_'.$model['table'])->where($condition)->find(); 

    }

    //获取字段显示
    public function get_list_model($type,$str,$config){
        switch ($type) {
            case '1':
            case '2':
            case '4':
                return $str;
                break;
            case '3':
                return html_out($str);
                break;
            case '5':
                if(!empty($str)){
                    $array=unserialize($str);
                    if(!empty($array)){
                        foreach ($array as $value) {
                            $strs.=$value['url'].'<br>';
                        }
                    }
                }
                return $strs;
                break;
            case '6':
            case '8':
                $list=explode("\n",html_out($config));
                foreach ($list as $key) {
                    $value=explode('|',$key);
                    if($value[1]==intval($str)){
                        return $value[0];
                    }
                }
                break;

            case '7':
                return date('Y-m-d H:i:s',$str);
                break;
            case '9':
                $list=explode("\n",html_out($config));
                foreach ($list as $key) {
                    $value=explode('|',$key);
                    if($value[1]==intval($str)){
                        $strs.=$value[0].' ';
                    }
                }
                return $strs;
                break;
            case '10':
                return '<img name="" src="'.$str.'" alt="" style="max-width:170px; max-height:90px; _width:170px; _height:90px;" />';
                break;
            default:
                return $str;
                break;
        }

    }

    //获取布局HTML
    public function get_layout_html($field_info,$data=null,$html=null){
        if(empty($html)){
            $html='<div class="formitm">
          <label class="lab" ><{name}>：</label>
          <div class="ipt">
            <{html}>
          <span class="Validform_checktip"><{tip}></span>
          </div></div>';
        }
        $html=str_replace('<{name}>', $field_info['name'], $html);
        $html=str_replace('<{html}>', $this->get_field_html($field_info,$data),$html );
        $html=str_replace('<{tip}>', $field_info['tip'],$html );
        $html.=$this->get_field_js($field_info);
        return $html;
    }

    //获取字段JS
    public function get_field_js($info){
        $html='';
        switch ($info['type']) {
            case '3':
                $html.=api('editor','get_editor',$info['field']);
                break;
            case '4':
                $html.=api('editor','get_upload_file',array('button'=>$info['field'].'_button','id'=>$info['field']));
                break;
            case '5':
                $html.=api('editor','get_upload_images',array('button'=>$info['field'].'_button','id'=>$info['field']));
                break;
            case '7':
                $config=explode("\n", $info['config']);
                if(empty($config[1])){
                    $jstime='yyyy-MM-dd HH:mm:ss';
                }
                $html.='<script>';
                $html.='Do.ready("calendar", function() {';
                $html.="    $('#".$info['field']."_button').calendar({ id:'#".$info['field']."',format:'".$jstime."'});";
                $html.='});';
                $html.='</script>';
                break;
             case '10':
                $html.=api('editor','get_upload_image',array('button'=>$info['field'].'_button','id'=>$info['field']));
                $html.=api('editor','get_image_cutout',array('button'=>$info['field'].'_cutout','id'=>$info['field']));
                break;
        }
        return $html;
    }

    //获取字段HTML
    public function get_field_html($info,$data=null){

        if(!empty($data)){
            $info['default']=$data;
        }
        $info['verification']=unserialize($info['verification']);
        if(!empty($info['verification'])){
            $verification='datatype="'.$info['verification'].'" errormsg="'.$info['verification_msg'].' " ';
        }

        $html='';
        switch ($info['type']) {
            case '1':
                $html.='<input name="'.$info['field'].'" type="text" class="u-ipt" id="'.$info['field'].'" value="'.$info['default'].'" '.$verification.'  />';
                break;
            case '2':
                $html.='<textarea name="'.$info['field'].'" class="u-itt" id="'.$info['field'].'"  '.$verification.' >'.$info['default'].'</textarea>';
                break;
            case '3':
                $html.='<textarea name="'.$info['field'].'" class="u-editor" id="'.$info['field'].'"  style="width:100%; height:250px;">'.$info['default'].'</textarea>';
                
                break;
            case '4':
                $html.='<input name="'.$info['field'].'" type="text" class="u-ipt" id="'.$info['field'].'" value="'.$info['default'].'"  '.$verification.' />';
                $html.='<span class="u-ico f-csp" id="'.$info['field'].'_button"><span class=" icon">&#xf0c6;</span> 上传文件</span>';
                
                break;
            case '5':
                $info['default']=unserialize($info['default']);
                $html.='<div><button class="u-btn u-btn-c4" id="'.$info['field'].'_button" type="button">上传图片</button></div>
                <div class="u-images" id="'.$info['field'].'_list"> <ul>';
                if(!empty($info['default'])){
                    foreach ($info['default'] as $value) {
                        $html.='
                        <li>
                            <div class="pic">
                                <img src="'.$value['url'].'" />
                            </div>
                            <input  id="'.$info['field'].'[]" name="'.$info['field'].'[]" type="hidden" value="'.$value['url'].'" />
                            <input  id="'.$info['field'].'_original[]" name="'.$info['field'].'_original[]" type="hidden" value="'.$value['original'].'" />
                            <div>描述：<input name="'.$info['field'].'_title[]" id="'.$info['field'].'_title[]" type="text" class="u-ipt" style="width:80px !important" id="" value="'.$value['title'].'" /></div>
                            <div>顺序：<input name="'.$info['field'].'_order[]" id="'.$info['field'].'_order[]" type="text" class="u-ipt" style="width:40px !important" id="" value="'.$value['order'].'" />&nbsp;&nbsp;<a href="javascript:void(0);" onclick="$(this).parent().parent().remove()">删除</a></div>
                        </li>';
                    }
                }
                $html.='</ul>
                <div class="f-cb"></div>
                </div>';
                break;
            case '6':
                $list=explode("\n",html_out($info['config']));
                if(empty($list)){
                    return ;
                }
                $html.='<div class="txt"> ';
                $html.='<select name="'.$info['field'].'" id="'.$info['field'].'">';

                foreach ($list as $key) {
                    $value=explode('|',$key);
                    $name=$value[0];
                    $val=intval($value[1]);
                    if($info['default']==$val){
                        $selected='selected="selected" ';
                    }else{
                        $selected='';
                    }
                    $html.='<option '.$selected.' value="'.$val.'">'.$name.'</option>';
                }
                $html.='</select>';
                $html.='</div>';
                break;
            case '7':
                $config=explode("\n", $info['config']);
                if(empty($config[0])){
                    $phptime='Y-m-d H:i:s';
                }
                if($info['default']){
                    $info['default']=date($phptime,intval($info['default']));
                }else{
                    $info['default']=date($phptime);
                }
                $html.='<input name="'.$info['field'].'" type="text" class="u-ipt" id="'.$info['field'].'" value="'.$info['default'].'" '.$verification.' /><span id="'.$info['field'].'_button" class="u-ico f-csp" id="corol_button"><span class="icon">&#xf017;</span> 选择</span>';
                
                break;
            case '8':
                $list=explode("\n",html_out($info['config']));
                if(empty($list)){
                    return ;
                }
                if(empty($info['default'])){
                    $info['default']=1;
                }
                $html.='<div class="txt"> ';
                foreach ($list as $key) {
                    $value=explode('|',$key);
                    $name=$value[0];
                    $val=intval($value[1]);
                    if(intval($info['default'])==$val){
                        $selected='checked="checked" ';
                    }else{
                        $selected='';
                    }
                    $html.='<input '.$selected.' name="'.$info['field'].'" type="radio" value="'.$val.'" /> '.$name.'&nbsp;&nbsp;';
                }
                $html.='</div>';
                break;
            case '9':
                $list=explode("\n",html_out($info['config']));
                if(empty($list)){
                    return ;
                }
                if(!empty($data)){
                   $default=unserialize($info['default']);
                }else{
                   $default=explode('|', $info['default']);
                }
                $html.='<div class="txt"> ';
                foreach ($list as $key) {
                    $value=explode('|',$key);
                    $name=$value[0];
                    $val=intval($value[1]);
                    if(in_array(intval($val), (array) $default)){
                        $selected.='checked="checked" ';
                    }else{
                        $selected='';
                    }
                    $html.='<input '.$selected.' name="'.$info['field'].'[]" type="checkbox" value="'.$val.'" /> '.$name.'&nbsp;&nbsp;';
                }
                $html.='</div>';
                break;
            case '10':
                $html.='<input name="'.$info['field'].'" type="text" class="u-ipt" id="'.$info['field'].'" value="'.$info['default'].'"  '.$verification.' />';
                $html.='<span class="u-ico f-csp" id="'.$info['field'].'_button"><span class=" icon">&#xf0c6;</span> 上传图片</span><span class="u-ico f-csp" id="'.$info['field'].'_cutout"><span class=" icon">&#xf03e;</span>处理</span>';
                break;
            case '11':
                $html.='<input name="'.$info['field'].'" type="text" class="u-ipt" id="'.$info['field'].'" value="'.$info['default'].'"  '.$verification.' /><span class="u-ico" id="corol_button"><span class="icon">&#xf0d6;</span></span>';
                break;
        }
        return $html;
    }

    //获取页面完整显示
    public function get_field_content($cid,$aid=null,$layout_html=null,$external=false) {
        $category_info=api('admin','category_info',$cid);
        if(empty($category_info['expand'])){
            return;
        }
        //获取字段列表
        $list=$this->field_list($category_info['expand']);
        if(empty($list)){
            return;
        }
        $html='';
        $js='';
        $info=$this->get_file_content($aid,$category_info['expand']);
        foreach ($list as $value) {
            if($external){
                if($value['delivery']){
                    $field_info=$this->field_info_id($value['fid']);
                    $html.=$this->get_layout_html($field_info,$info[$value['field']],$layout_html);
                }else{
                    continue;
                }
            }else{
                $field_info=$this->field_info_id($value['fid']);
                $html.=$this->get_layout_html($field_info,$info[$value['field']],$layout_html);
            }
        }
        echo $html;
    }

    //格式化录入字段内容
    public function field_in($data,$type,$field) {
        switch ($type) {
            case '1':
            case '4':
                return in($data[$field]);
                break;
            case '2':
            case '3':
                return html_in($data[$field]);
                break;
            case '5':
                if(is_array($data[$field])){
                    $str1=$field.'_title';
                    $str2=$field.'_order';
                    $str3=$field.'_original';
                    $title=$data[$str1];
                    $order=$data[$str2];
                    $original=$data[$str3];
                   foreach ($data[$field] as $key=>$vo) {
                        $list[$key]['url']=$vo;
                        $list[$key]['original']=$original[$key];
                        $list[$key]['title']=$title[$key];
                        $list[$key]['order']=$order[$key];
                   }
                }
                return serialize($list);
                break;
            case '6':
            case '8':
                return intval($data[$field]);
                break;
            case '7':
                return strtotime($data[$field]);
                break;
            case '9':
                return serialize($data[$field]);
                break;
            default:
                return in($data[$field]);
                break;
        }
    }

    //检测扩展字段是否必填
    public function content_check($data)
    {
        //获取栏目信息
        $category=api('admin','category_info',$data['cid']);
        if(empty($category['expand'])){
            return;
        }
        //获取扩展模型信息
        $model=model('expand_model')->info($category['expand']);
        if(empty($model)){
            return;
        }
        //获取字段列表
        $field_list=$this->field_list($model['mid']);
        if(empty($field_list)){
            return;
        }
        foreach ($field_list as $value) {
            $value['verification']=unserialize($value['verification']);
            if(!empty($value['verification'])){
                if(empty($data[$value['field']])){
                    $this->msg($value['name'].'不能为空！',0);
                }
            }
        }
    }

    //保存内容数据
    public function content_save($data)
    {
        //获取栏目信息
        $category=api('admin','category_info',$data['cid']);
        if(empty($category['expand'])){
            return;
        }
        //获取扩展模型信息
        $model=model('expand_model')->info($category['expand']);
        if(empty($model)){
            return;
        }
        //获取字段列表
        $field_list=$this->field_list($model['mid']);
        if(empty($field_list)){
            return;
        }
        $data_save=array();
        foreach ($field_list as $value) {
            $data_save[$value['field']]=$this->field_in($data,$value['type'],$value['field']);
        }
        $data_save['aid']=$data['aid'];
        $this->table = 'expand_content_'.$model['table'];
        $condition['aid']=$data['aid'];
        if($this->count($condition)){
            return $this->update($condition,$data_save); 
        }else{
            return $this->insert($data_save);
        }
    }

    //删除内容数据
    public function content_del($aid){
        if(empty($aid)){
            return;
        }
        //获取内容信息
        $content_info=api('admin','content_info',$aid);
        if(empty($content_info)){
            return;
        }
        //获取栏目信息
        $category=api('admin','category_info',$content_info['cid']);
        if(empty($category['expand'])){
            return;
        }
        //获取扩展模型信息
        $model=model('expand_model')->info($category['expand']);
        //删除操作
        $condition['aid']=$aid;
        $this->table = 'expand_content_'.$model['table'];
        return $this->delete($condition); 
    }
    

    
}

?>